<template>
	<view>
		<view v-if="shangxian === '否'">
			<view class="padding">
				<view class="text-center padding-bottom text-red" v-if="status==2">
					拒绝理由：{{form.checkCertificationMessage}}
				</view>
				<view class="text-center padding-bottom text-red" v-if="status==0">审核中</view>
				<view class=" padding-lr bg-white " style="border-radius: 24rpx;">
					<u-form :model="form" ref="uForm" label-position="top" :label-style='lableStyle'>
						<u-form-item label="真实姓名" :border-bottom='true'>
							<u-input placeholder="请输入真实姓名" v-model="form.realName" />
						</u-form-item>
						<u-form-item label="证件号码" :border-bottom='false'>
							<u-input placeholder="请填写（必填） " v-model="form.identityCardNumber" />
						</u-form-item>
						<u-form-item label="出生日期" :border-bottom='true'>
							<u-input placeholder="请填写（必填）" v-model="form.birthMention" :disabled="true"
								@click="show = true" />
						</u-form-item>
						<u-form-item label="驾龄(年)" :border-bottom='false'>
							<u-input placeholder="请填写（必填） " v-model="form.drivingYear" />
						</u-form-item>
						<u-form-item label="省市区" :border-bottom='false'>
							<u-input placeholder="请选择位置（必填） " v-model="cityqu" :disabled="true" @click="openMap()" />
						</u-form-item>
					</u-form>
				</view>
				<view class=" padding bg-white margin-top " style="border-radius: 24rpx;">
					<view class="text-bold text-black">驾驶证及保险</view>
					<view>
						<view class="flex" style="overflow: hidden;flex-wrap: wrap;">
							<view v-if="drivingLicence.length">
								<view class="margin-top-sm flex margin-right-sm flex-wrap">
									<view v-for="(item,index) in drivingLicence" :key="index">
										<view class="flex"
											style="width: 200rpx;height: 200rpx;margin-right: 2rpx;position: relative;">
											<image :src="item" style="width: 100%;height: 100%;"></image>
											<view style="z-index: 9;position: absolute;top: -15rpx;right: -15rpx;"
												@click="removeImg(index)">
												<image src="../static/gb.png" style="width:50rpx;height:50rpx;"></image>
											</view>

										</view>
									</view>
								</view>
							</view>
							<view class="margin-top-sm" @click="addImage()" v-if="drivingLicence.length<=5">
								<view style="width: 200rpx;height: 200rpx;background: #f4f5f6;border-radius: 16rpx;"
									class="flex justify-center align-center">
									<view>
										<view class="text-center">
											<image src="../static/photo.png" style="width: 65rpx;height: 55rpx;">
											</image>
										</view>
										<view class="text-center text-black">上传驾驶证</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class=" padding bg-white  margin-top" style="border-radius: 24rpx;">
					<view class="text-bold text-black">上传身份证件</view>
					<view class="margin-top"
						style="border: 2rpx dashed #484B74; width: 100%;height: 320rpx;position: relative;">
						<view style="text-align: center;margin: 80rpx auto 0;" @click="addImages(1)"
							v-if="!form.identityCardFront">
							<image src="../static/photo.png" mode="widthFix" style="width: 73rpx;"></image>
							<view class="text-sm text-gray margin-top-sm">请拍摄身份证正面</view>
						</view>
						<image @click="disabled? '':addImages(1)" v-else :src="form.identityCardFront"
							style="width: 100%;height: 320rpx;" mode="aspectFill"></image>
					</view>
					<!-- <view class="text-lg">上传身份证反面</view> -->
					<view class="margin-top"
						style="border: 2rpx dashed #484B74; width: 100%;height: 320rpx;position: relative;">
						<view style="text-align: center;margin: 80rpx auto 0;" @click="addImages(2)"
							v-if="!form.identityCardVerso">
							<image src="../static/photo.png" mode="widthFix" style="width: 73rpx;"></image>
							<view class="text-sm text-gray margin-top-sm">请拍摄身份证反面</view>
						</view>
						<image @click="disabled? '':addImages(2)" v-else :src="form.identityCardVerso"
							style="width: 100%;height: 320rpx;" mode="aspectFill"></image>
					</view>
				</view>
				<view class="padding bg-white radius margin-top" v-if="!disabled&&DepositSwitch=='是'">
					<view class="text-bold" style="color: #3699FF;">缴纳保证金</view>
					<view class="flex justify-between margin-top-sm">
						<view class="text-gray">满足订单量及好评率即可申请退还保证金</view>
						<view class="text-bold" style="color:#F1413D;">¥{{money}}</view>
					</view>
				</view>
				<view class=" margin-top footer text-xxl" v-if="!disabled">
					<text @tap="isShowAgree" class="cuIcon"
						:class="showAgree?'cuIcon-radiobox':'cuIcon-round'">同意并接受</text>
					<!-- 协议地址 -->
					<navigator url="/my/setting/sijixieyi" style="color: #3699FF;" open-type="navigate">《师傅入驻协议 》
					</navigator>

				</view>
				<view class=" margin-top footer text-xxl" v-if="disabled">
					查看
					<navigator url="/my/setting/sijixieyi" style="color: #3699FF;" open-type="navigate">《师傅入驻协议 》
					</navigator>
				</view>
				<view style="height: 120rpx;"></view>
			</view>
			<view class="bg-white flex justify-between align-center padding-lr " v-if="!disabled"
				style="position: fixed;bottom: 0;width: 100%;height: 120rpx;z-index: 99;"
				:style="DepositSwitch!='是'?'justify-content:end':''">
				<view class="" v-if="DepositSwitch=='是'">
					<text class="text-lg text-bold text-black">支付：</text>
					<text style="color: #F1413D;">¥{{money}}</text>
				</view>
				<view>
					<u-button v-if="!disabled" @click="submit" class="" :custom-style="customStyle" shape="circle"
						:hair-line="false">入驻</u-button>
				</view>
			</view>

			<u-picker v-model="show" mode="time" @confirm="confirm"></u-picker>

			<u-popup v-model="showpay" mode="bottom">
				<view class="popup_pay">
					<view class="bg margin-top padding-lr radius">
						<view style="padding: 0 20upx;margin-top: 36rpx;">
							<view
								style="display: flex;height: 100upx;align-items: center;padding: 20upx 0;justify-content: center;"
								v-for="(item,index) in openLists" :key='index'>
								<image :src="item.image" style="width: 55upx;height: 55upx;border-radius: 50upx;">
								</image>
								<view style="font-size: 30upx;margin-left: 20upx;width: 70%;">
									{{item.name}}
								</view>
								<radio-group name="openWay" style="margin-left: 45rpx;" @tap='selectWay(item)'>
									<label class="tui-radio">
										<radio color="#3699FF" :checked="openWay == item.id ? true : false" />
									</label>
								</radio-group>
							</view>
						</view>
					</view>
					<view class="pay_btns" @click="pay()">确认支付</view>
				</view>
			</u-popup>
		</view>

		<view v-if="shangxian === '是'">
			<view style="line-height: 26px;padding: 32upx;" class="home1">
				<view style="font-size: 28upx;" v-html="content"></view>
			</view>
		</view>
	</view>
</template>

<script>
	import configdata from '@/common/config.js';
	export default {
		data() {
			return {
				content: '',
				form: {
					realName: '',
					identityCardNumber: '', //证件号码
					birthMention: '', //出生日期
					identityCardFront: '', //身份证正面
					identityCardVerso: '', //身份证反面
					drivingLicence: '', //驾驶证
					drivingYear: '', //驾龄
					checkCertificationMessage: '',
					userType: 2,
					province: '', // 接单省
					city: '', // 接单市
					district: '', // 接单区
				},
				drivingLicence: [],
				lableStyle: {
					color: '#000',
					fontSize: '28upx',
					fontWeight: 500,
				},
				customStyle: {
					backgroundColor: '#3699FF',
					color: '#FFFFFF',
					border: 0,
					width: '200rpx'
				},
				btnStyle: {
					backgroundColor: '#3699FF',
					color: '#FFFFFF',
				},
				money: 0,
				showAgree: false, //协议是否选择
				// sessionkey: '',
				shangxian: '',
				showpay: false,
				openWay: 1,
				openLists: [],
				indentNumber: '',
				disabled: false,
				show: false,
				status: -1,

				cityqu: '',
				latitude: '',
				longitude: '',
				DepositSwitch: ''

			}
		},
		onLoad() {
			this.shangxian = uni.getStorageSync('shangxian')
			this.DepositSwitch = uni.getStorageSync('DepositSwitch')
			if (this.shangxian != '是') {
				uni.setNavigationBarTitle({
					title: '实名认证'
				})
			} else {
				uni.setNavigationBarTitle({
					title: '关于我们'
				})

				this.$Request.getT('/app/userinfo/userAgreement').then(res => {
					if (res.code == 0) {
						this.content = res.data.value;
						// this.tit = res.data.min
					}
				});
			}
			// #ifdef MP-WEIXIN
			this.openLists = [{
				id: 1,
				image: '../../static/image/icon_weixin.png',
				name: '微信'
			}]
			// #endif

			// #ifdef H5
			let ua = navigator.userAgent.toLowerCase();
			if (ua.indexOf('micromessenger') !== -1) {
				//公众号是否自动登录  416
				this.$Request.get('/app/common/type/416').then(res => {
					if (res.data && res.data.value && res.data.value == '是') {
						this.openLists = [{
							id: 2,
							image: '../../static/image/zhifubao.png',
							name: '支付宝'
						}, {
							id: 1,
							image: '../../static/image/icon_weixin.png',
							name: '微信'
						}];
						this.openWay = 2;
					} else {
						this.openLists = [{
							id: 2,
							image: '../../static/image/zhifubao.png',
							name: '支付宝'
						}];
						this.openWay = 1;
					}
				})
			} else {
				this.openLists = [{
					id: 2,
					image: '../../static/image/zhifubao.png',
					name: '支付宝'
				}];
				this.openWay = 1;
			}
			// #endif

			// #ifdef APP-PLUS
			this.openLists = [{
					id: 1,
					image: '../../static/image/icon_weixin.png',
					name: '微信'
				},
				{
					id: 2,
					image: '../../static/image/zhifubao.png',
					name: '支付宝'
				}
			]
			// #endif

			this.getRZmoney()
			this.getUserInfo()
			// this.sessionkey = this.$queue.getData("sessionkey");
		},
		methods: {
			openMap() {
				let that = this
				uni.chooseLocation({
					success: function(res) {
						console.log('位置名称：' + res.name);
						console.log('详细地址：' + res.address);
						console.log('纬度：' + res.latitude);
						console.log('经度：' + res.longitude);
						that.latitude = res.latitude
						that.longitude = res.longitude
						that.selectCity(that.longitude, that.latitude);
					}
				});

			},
			//设置地图标记
			selectCity(longitude, latitude) {

				this.$Request.getT('/app/Login/selectCity?lat=' + latitude + '&lng=' + longitude).then(res => {
					if (res.code == 0) {
						this.form.city = res.data.city ? res.data.city : res.data.province
						this.form.county = res.data.district
						this.form.province = res.data.province
						this.cityqu = this.form.province + ' ' + this.form.city + ' ' + this.form.county

					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},

			confirm(e) {
				// console.log(e,'========')
				this.form.birthMention = e.year + '-' + e.month + '-' + e.day
			},
			removeImg(index) {
				this.drivingLicence.splice(index, 1)
			},
			isShowAgree() {
				this.showAgree = !this.showAgree
			},
			getRZmoney() {
				this.$Request.get("/app/common/type/261").then(res => {
					if (res.code == 0) {
						this.money = res.data.value
						// this.form.cashDeposit = res.data.value
					}
				});
			},
			selectWay: function(item) {
				this.openWay = item.id;
			},
			submit() {
				let that = this
				// console.log(that.form)
				if (!that.form.realName) {
					uni.showToast({
						title: '请输入真实姓名',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.form.identityCardNumber) {
					uni.showToast({
						title: '请输入身份证号',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.form.birthMention) {
					uni.showToast({
						title: '请选择出生日期',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.form.drivingYear) {
					uni.showToast({
						title: '请填写驾龄',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.cityqu) {
					uni.showToast({
						title: '请选择省市区',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.form.identityCardFront) {
					uni.showToast({
						title: '请上传身份证正面',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.form.identityCardVerso) {
					uni.showToast({
						title: '请上传身份证反面',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (that.drivingLicence.length == 0) {
					uni.showToast({
						title: '请上传驾驶证',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				if (!that.showAgree) {
					uni.showToast({
						title: '请勾选师傅入驻协议',
						icon: 'none',
						duration: 1000
					})
					return;
				}
				let drivingLicence = that.drivingLicence
				that.form.drivingLicence = drivingLicence.toString()
				if (that.DepositSwitch == '是') {
					that.showpay = true
				} else {
					uni.showLoading({
						title: '提交中...'
					})
					that.$Request.postJson("/app/user/riderCertification", that.form).then(ret => {
						if (ret.code == 0) {
							uni.hideLoading();
							uni.showToast({
								title: '实名认证提交成功',
								icon: 'none'
							});
							setTimeout(function() {
								uni.navigateBack(1)
							}, 1000)
						} else {
							uni.showToast({
								title: ret.msg,
								icon: 'none'
							})
						}
					});
				}

			},
			getUserInfo() {
				this.$Request.getT("/app/userinfo/findUserInfoById").then(res => {
					if (res.code == 0 && res.data) {
						this.form.realName = res.data.realName ? res.data.realName : ''
						this.form.identityCardNumber = res.data.identityCardNumber ? res.data.identityCardNumber :
							''
						this.form.birthMention = res.data.birthMention ? res.data.birthMention : ''
						this.form.identityCardVerso = res.data.identityCardVerso ? res.data.identityCardVerso : ''
						this.form.identityCardFront = res.data.identityCardFront ? res.data.identityCardFront : ''
						this.form.drivingYear = res.data.drivingYear ? res.data.drivingYear : ''
						this.form.drivingLicence = res.data.drivingLicence ? res.data.drivingLicence : ''
						if (res.data.drivingLicence) {
							this.drivingLicence = res.data.drivingLicence.split(',')
						}

						this.form.city = res.data.city ? res.data.city : res.data.province
						this.form.county = res.data.district ? res.data.district : ''
						this.form.province = res.data.province ? res.data.province : ''
						if (this.form.city) {
							this.cityqu = this.form.province + ' ' + this.form.city + ' ' + this.form.county
						}


						if (res.data.checkCertification == 1 || res.data.checkCertification == 0) {
							this.disabled = true
						}

						if (res.data.checkCertification == 2) {
							this.form.checkCertificationMessage = res.data.checkCertificationMessage ? res.data
								.checkCertificationMessage : ''
						}
						this.status = res.data.checkCertification
					}
				});
			},
			addImages(e) {
				uni.chooseImage({
					count: 1,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < 1; i++) {
							this.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
								url: 'https://shifu.tadaijia.cn/sqx_fast/alioss/upload',
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									console.log(uploadFileRes)
									if (e == 1) {
										this.form.identityCardFront = JSON.parse(uploadFileRes
											.data).data
									} else if (e == 2) {
										this.form.identityCardVerso = JSON.parse(uploadFileRes
											.data).data
									}
									uni.hideLoading();
								}
							});
						}
					}
				})
			},
			addImage() {
				let count = 6 - this.drivingLicence.length
				uni.chooseImage({
					count: count,
					sourceType: ['album', 'camera'],
					success: res => {
						for (let i = 0; i < res.tempFilePaths.length; i++) {
							this.$queue.showLoading("上传中...");
							uni.uploadFile({ // 上传接口
								// url: this.config("APIHOST1") + '/alioss/upload', //真实的接口地址
								url: 'https://shifu.tadaijia.cn/sqx_fast/alioss/upload',
								filePath: res.tempFilePaths[i],
								name: 'file',
								success: (uploadFileRes) => {
									// console.log(uploadFileRes)
									this.drivingLicence.push(JSON.parse(uploadFileRes.data).data)
									uni.hideLoading();
								}
							});
						}
					}
				})
			},
			config: function(name) {
				var info = null;
				if (name) {
					var name2 = name.split("."); //字符分割
					if (name2.length > 1) {
						info = configdata[name2[0]][name2[1]] || null;
					} else {
						info = configdata[name] || null;
					}
					if (info == null) {
						let web_config = cache.get("web_config");
						if (web_config) {
							if (name2.length > 1) {
								info = web_config[name2[0]][name2[1]] || null;
							} else {
								info = web_config[name] || null;
							}
						}
					}
				}
				return info;
			},
			pay() {
				if (this.openWay == 0) {
					this.$queue.showToast('请选择支付方式!')
					return;
				}
				this.form.drivingLicence = this.drivingLicence
				this.form.drivingLicence = this.form.drivingLicence.toString()
				if (this.openWay == 1) {
					console.log('微信')
					// #ifdef APP-PLUS
					// 微信APP支付 根据订单id获取支付信息
					this.$Request.postJson("/app/wxPayRider/appRiderCertification", this.form).then(res => {
						console.log(JSON.stringify(res), '微信支付信息')
						this.isCheckPay(res.code, 'wxpay', JSON.stringify(res.data));
					});
					// #endif
					// #ifdef H5
					// 微信APP支付 根据订单id获取支付信息
					this.$Request.postJson("/app/wxPayRider/wxPayMpRiderCertification", this.form).then(res => {
						console.log(JSON.stringify(res), '微信支付信息')
						this.callPay(res.data);
					});
					// #endif
					// #ifdef MP-WEIXIN
					let that = this
					that.$Request.postJson("/app/wxPayRider/wxPayJsApiRiderCertification", that.form).then(res => {
						if (res.code == 0) {
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.timestamp,
								nonceStr: res.data.noncestr,
								package: res.data.package,
								signType: res.data.signType,
								paySign: res.data.sign,
								success: function(res) {
									console.log(res)
									uni.hideLoading();
									uni.showToast({
										title: '实名认证提交成功',
										icon: 'none'
									});
									setTimeout(function() {
										uni.navigateBack(1)
									}, 1000)
								},
								fail: function(err) {
									console.log('fail:' + JSON.stringify(err));
									uni.hideLoading();
									that.$queue.showToast('支付失败');
								}
							});
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
						}
					});
					// #endif
				} else {
					// APP支付宝支付
					// #ifdef H5
					this.form.payType = 5
					this.$Request.postJson('/app/aliPay/aliPayRiderCertification', this.form).then(
						res => {
							if (res.code == 0) {
								const div = document.createElement('div')
								div.innerHTML = res.data //此处form就是后台返回接收到的数据
								document.body.appendChild(div)
								document.forms[0].submit()
							} else {
								uni.showToast({
									icon: 'none',
									title: '支付失败!'
								});
							}
						});
					// #endif

					// #ifdef APP
					this.form.payType = 4
					this.$Request.postJson("/app/aliPay/aliPayRiderCertification", this.form).then(res => {
						console.log(JSON.stringify(res), '支付宝支付信息')
						this.isCheckPay(res.code, 'alipay', res.data)
					});
					// #endif
				}
			},
			callPay: function(response) {
				if (typeof WeixinJSBridge === "undefined") {
					if (document.addEventListener) {
						document.addEventListener('WeixinJSBridgeReady', this.onBridgeReady(response), false);
					} else if (document.attachEvent) {
						document.attachEvent('WeixinJSBridgeReady', this.onBridgeReady(response));
						document.attachEvent('onWeixinJSBridgeReady', this.onBridgeReady(response));
					}
				} else {
					this.onBridgeReady(response);
				}
			},
			onBridgeReady: function(response) {
				let that = this;
				// if (!response.package) {
				// 	return;
				// }
				console.log(response, 111111)
				WeixinJSBridge.invoke(
					'getBrandWCPayRequest', {
						"appId": response.appid, //公众号名称，由商户传入
						"timeStamp": response.timestamp, //时间戳，自1970年以来的秒数
						"nonceStr": response.noncestr, //随机串
						"package": response.package,
						"signType": response.signType, //微信签名方式：
						"paySign": response.sign //微信签名
					},
					function(res) {
						console.log(res, '/*-/*-/*-')
						if (res.err_msg === "get_brand_wcpay_request:ok") {
							// 使用以上方式判断前端返回,微信团队郑重提示：
							//res.err_msg将在用户支付成功后返回ok，但并不保证它绝对可靠。
							uni.showLoading({
								title: '支付成功'
							});
							setTimeout(function() {
								uni.hideLoading();
								uni.navigateBack()
							}, 1000);
						} else {
							uni.hideLoading();
						}
						WeixinJSBridge.log(response.err_msg);
					}
				);
			},
			isCheckPay(status, name, order) {
				if (status == 0) {
					this.setPayment(name, order);
				} else {
					uni.hideLoading();
					uni.showToast({
						title: '支付信息有误',
						icon: 'none'
					});
				}
			},
			setPayment(name, order) {
				console.log(name, '*-*-*', order)
				uni.requestPayment({
					provider: name,
					orderInfo: order, //微信、支付宝订单数据
					success: function(res) {
						console.log(res)
						uni.hideLoading();
						uni.showLoading({
							title: '支付成功'
						});
						setTimeout(function() {
							uni.navigateBack()
						}, 1000);
					},
					fail: function(err) {
						console.log(err)
						uni.hideLoading();
					},
					complete() {
						uni.hideLoading();
					}
				});
			},
		}
	}
</script>

<style lang="less">
	page {
		background: #F5F5F5;
	}

	.footer {
		/* // padding-left: 140upx; */
		/* justify-content: center; */
		margin-top: 32upx;
		font-size: 24upx;
		color: #666666;
		display: flex;
		// text-align: center;
		// align-items: center;
	}

	/* 支付弹框 */
	.popup_pay {
		width: 100%;
	}

	.pay_btns {
		width: 90%;
		margin: 0 auto 40rpx;
		text-align: center;
		background: #3699FF;
		height: 80rpx;
		border-radius: 16rpx;
		color: #ffffff;
		line-height: 80rpx;
		margin-top: 20rpx;
	}
</style>